﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Battleborn.Core.Server;
using Battleborn.Core.ViewModels;
using Microsoft.AspNet.SignalR;

namespace Battleborn.Website.Hubs
{
    public class Chat : Hub
    {
        private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();

        public void Send(string message)
        {
            string name = Context.QueryString["name"];

            Clients.All.send(name + ": " + message);
        }

        public void Send(string who, string message)
        {
            string name = Context.QueryString["name"];

            foreach (var connectionId in _connections.GetConnections(who))
            {
                Clients.Client(connectionId).send(name + ": " + message);
            }
        }

        //public UserViewModel GetUserInfo()
        //{
        //    var userId = Context.User.Identity.Name;

        //    //ChatUser user = _repository.VerifyUserId(userId);

        //    return new UserViewModel(user);
        //}

        public override Task OnConnected()
        {
            string name = Context.QueryString["name"];

            _connections.Add(name, Context.ConnectionId);

            return base.OnConnected();
        }

        public override Task OnDisconnected()
        {
            string name = Context.QueryString["name"];

            _connections.Remove(name, Context.ConnectionId);

            return base.OnDisconnected();
        }  
    }
}